package com.soshalboard.remote;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.soshalboard.config.ConfigHolder;
import com.soshalboard.persistence.DBPersister;
import com.soshalboard.persistence.domain.Category;
import com.soshalboard.persistence.domain.Product;
import com.soshalboard.persistence.domain.Session;
import com.soshalboard.persistence.domain.User;
import com.soshalboard.persistence.domain.WishList;
import com.soshalboard.remote.interfaces.APIInterface;
import com.soshalboard.utils.CreateExcel;

/*
 * Implementation class of APIInterface and SessionHolder interfaces.
 * Initialize various managers related to various services that simulator provides.
 * Implementation of session handlers.
 */
public class APIImpl implements APIInterface {
	private DBPersister dbPersister;
	private ConfigHolder configHolder;
	private CreateExcel createExcel;
	private static Log log = LogFactory.getLog(APIInterface.class);
	private static Log oLog = LogFactory.getLog(APIImpl.class);

	/*
	 * Constructor use to initialize various managers and DBPersister variables.
	 */
	public APIImpl(DBPersister dp, ConfigHolder ch, CreateExcel cExcel) {
		this.dbPersister = dp;
		this.configHolder = ch;
		this.createExcel = cExcel;
	}

	@Override
	public User getUser(Integer userId) {
		User user = new User ();
		user.setAccessToken("sess1");
		user.setFirstName("Rajesh");
		user.setLastName("Vashista");
		user.setUserName("rajesh_vashista");
		
		return user;
	}

	@Override
	public User createOrUpdateUser(User user) {
		
		return dbPersister.insertOrUpdateUser(user);
	}	
	
	
	@Override
	public List<WishList> getWishListUsingFbId(Integer fbUserId, Integer page, Integer size) {
		Integer userId = dbPersister.getUserByFBId(fbUserId);
		return getWishList(userId, page, size);
	}
	
	@Override
	public List<WishList> getWishList(Integer id, Integer page, Integer size) {
		
		if (0 == id) {
			id = null;
		}
		
		List<WishList> wishList = new ArrayList<WishList>();
		wishList = dbPersister.getWishList(id, page, size);
		
		List<Integer> wishIds = new ArrayList<Integer>();
		for (WishList wish:wishList) {
			wishIds.add(wish.getCategory().getId());
		}
		
		Map<Integer, Integer> noWishes = dbPersister.getOtherUsersforWishList(wishIds);
		
		for (WishList wish:wishList) {
			wish.setNoOfUsersWished(noWishes.get(wish.getCategory().getId()));
		}		
		
 		return wishList;
	}

	@Override
	public void createOrUpdateWishList(WishList wishList) {
		dbPersister.createOrUpdateWishList(wishList);
		
	}

	@Override
	public Map<String, List<Category>> getCategories() {
		return dbPersister.getCategories();
	}

	@Override
	public void saveSession(Session session) {
		dbPersister.saveSession(session);
		
	}

	@Override
	public Session getSession(Integer userId) {
		return dbPersister.getSession(userId);
	}
	
	@Override
	public Session getSession(String sessionId) {
		return dbPersister.getSessionUsingSessionId(sessionId);
	}

	@Override
	public Category getCategory(Integer id) {
		return dbPersister.getCategoryById(id);
	}

	@Override
	public void createOrUpdateProduct(Product product) {
		dbPersister.createOrUpdateProduct(product);
		
	}

	@Override
	public List<Product> getProductsByCategory(Integer categoryId) {
		return dbPersister.getProductByCategory(categoryId);
	}
}
